package com.yoomiito.app.ui.fragment.cicle;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yoomiito.app.adapter.cicle.CicleAdapter;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.model.cicle.CicleInfo;
import com.yoomiito.app.present.cicle.CicleFmPresenter;
import com.yoomiito.app.ui.cicle.CiclePublishActivity;
import com.yoomiito.app.ui.fragment.my.MyOrderBaseFragment;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.L;
import com.yoomiito.app.utils.RouterUtils;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.utils.YMToast;

import java.util.ArrayList;
import java.util.List;

import cn.droidlover.xdroidmvp.net.ApiException;

/**
 * 首页--优米/瑰蜜圈---圈子交流
 */
public class CicleFragment extends MyOrderBaseFragment<CicleFmPresenter> {
//    @BindView(R.id.rv_order)
//    RecyclerView mRecyclerView;
    private CicleAdapter mAdapter;
    private List<CicleInfo.DataBean> mList;

    @Override
    public CicleFmPresenter newP() {
        return new CicleFmPresenter(App.mAppConfig);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mList = new ArrayList<>();

        mAdapter = new CicleAdapter(mList);
        mRecyclerView.setAdapter(mAdapter);
        getNewData();
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Intent intent = new Intent(getActivity(),MyHistoryMonthActivity.class);
//                intent.putExtra(Constant.KEY_MONTH,mList.get(position).getCreate_date_month());
//                startActivity(intent);
            }
        });
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            CicleInfo.DataBean dataBean = mAdapter.getData().get(position);
//            if (TextUtils.isEmpty(dataBean.getPrice()) ||
//                    "0".equals(dataBean.getPrice()) ||
//                    "0.0".equals(dataBean.getPrice()) ||
//                    "0.00".equals(dataBean.getPrice())) {
//                YMToast.showText("商品已下架");
//                return;
//            }
            if (SpannableUtils.isEmptyOr0(dataBean.getPrice())){
                YMToast.showText("商品已下架");
                return;
            }
            if (1 == dataBean.getOrder_type()) {
                RouterUtils.toGoodsDetail((BaseActivity) context, String.valueOf(dataBean.getProduct_id()), AllCode.TaoBao);
            } else {
                RouterUtils.toGoodsDetail((BaseActivity) context, String.valueOf(dataBean.getProduct_id()), AllCode.Gift);
            }
        });

    }

    @Override
    protected void getNewData() {
        getP().getCicleCommunicateData(currentPage);
    }

    @Override
    public String getEmptyContent() {
        return "未发布内容";
    }

    @Override
    public void handleError(ApiException e) {

    }

    public void setCicleData(CicleInfo data) {
        if (currentPage == 1) {
            if (data != null && data.getData() != null) {
                mAdapter.setNewData(data.getData());
                mSmartRefreshLayout.finishRefresh(true);
                mStatusManager.showContent();
                mStatusManager.showContent();
            }else {
                mStatusManager.showEmpty();
            }
        } else {
            mSmartRefreshLayout.finishLoadMore(true);
            if (data.getData() == null || data.getData().size() == 0) {
                mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                mAdapter.addData(data.getData());
            }
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode== CiclePublishActivity.CODE_PUBLISH_CICLE){
            currentPage=1;
            getP().getCicleCommunicateData(currentPage);
            L.i("刷新数据");
        }
    }
}
